<?php $this->_extends('../_layouts/default_layout');?>
<?php $this->_block('contents');?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript">
Refresh = function(){
	//jq的自动完成
	$(".ajax_product").autocomplete("<?=url('Purchase::Loss/ProductInfoAjax')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent();
			container.find("input.product_number").val(data['number']);
			container.find("input[name^='product_id']").val(data['id']);
		}
	});
};
$(function(){
	var i = 1;
	$("#add_product").click(function(){
		var _html = "<tr>";
		_html += "<td><input type='text' size='8' disabled='disabled' class='product_number' /><input type='text' size='50' name='product_number_new" + i + "' class='ajax_product' /><input type='hidden' name='product_id_new" + i + "' /></td>";
		_html += "<td><input type='text' size='8' name='price_new" + i + "' /></td>";
		_html += "<td><input type='text' size='8' name='quantity_new" + i + "' /></td>";
		_html += "<td><textarea rows='1' cols='30' name='reason_new" + i + "' ></textarea></td>";
		_html += "<td><a href='javascript:void(0);' class='del_product'>删除产品</a></td>";
		_html += "</tr>";
		$("#list").append(_html);
		Refresh();
		i++;
	});
	$(".del_product").live("click", function(){
		if ( ! confirm("确定删除该产品？"))
		{
			return false;
		}
		else
		{
			$(this).parent().parent().remove();
		}
	});
	//保存采购单
	$("#add_save").click(function(){
		var can_save = 1;
		//检查添加产品
		$("#loss_list").find("input[name^='product_id_new']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个产品编号");
				return false;
			}
		});
		$("#loss_list").find("input[name^='price_new']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个产品单价");
				return false;
			}
		});
		$("#loss_list").find("input[name^='quantity_new']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个产品数量");
				return false;
			}
		});
		if (can_save)
		{
			$("form").submit();
			$(this).attr('disabled', true);
		}
	});
	Refresh();
});
</script>
<form method="post" action="<?=url('Purchase::Loss/CreateSave')?>" id="loss_list">
<table width="80%" class="list_table" id="list">
	<tr>
		<th width="400">产品编号</th>
		<th width="80">单价</th>
		<th width="80">数量</th>
		<th width="200">损耗原因</th>
		<th width="100">操作</th>
	</tr>
	<tr>
		<td><input type="text" size="8" disabled="disabled" class="product_number" /><input type="text" size="50" name="product_number_new0" class="ajax_product" /><input type="hidden" name="product_id_new0" /></td>
		<td><input type="text" size="8" name="price_new0" /></td>
		<td><input type="text" size="8" name="quantity_new0" /></td>
		<td><textarea rows="1" cols="30" name="reason_new0"></textarea></td>
		<td><a href="javascript:void(0);" class="del_product">删除产品</a></td>
	</tr>
</table>
<a id="add_product">添加损耗产品</a>
<div style="width: 80%" align="center"><input type="button" id="add_save" class="button" value="提交" /><input type="hidden" name="<?=Q::ini('unique_token_name');?>" value="<?=Helper_BSS_Normal::genToken();?>"/></div>
</form>
<?php $this->_endblock();?>